package com.zzx.graduation.service;


import com.zzx.graduation.response.Constants;
import com.zzx.graduation.response.ResponseMessage;
import com.zzx.graduation.response.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;


@Component
public class FileService {

    private static final String staticPath = "src/main/resources/static/";



    public void fileDownload(String fileName, HttpServletResponse response) throws Exception {
       //静态文件获取方式
//        File fil = new File(staticPath + "text/111.txt");
//        System.out.println(fil.getAbsolutePath());
//        System.out.println(fil.exists());
        //去除 . 之后取得类型
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (!(Constants.IMAGE.equals(suffixName) || Constants.TEXT.equals(suffixName) ||
                Constants.VIDEO.equals(suffixName) )){
            suffixName = Constants.OTHER_FILE;
        }

        File file = new File( suffixName + File.separator + fileName);

        if (!file.exists()) {
            System.out.println(file.getAbsolutePath());
            throw new Exception(fileName + "文件不存在");
        }
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);

        byte[] buffer = new byte[1024];
        try (FileInputStream fis = new FileInputStream(file);
             BufferedInputStream bis = new BufferedInputStream(fis)) {

            OutputStream os = response.getOutputStream();

            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        }
    }

    public String upload(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            throw new Exception("未选择需上传的日志文件");
        }

        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);

        System.out.println(fileName + "-----" + suffixName );

        if (!(Constants.IMAGE.equals(suffixName) || Constants.TEXT.equals(suffixName) ||
                Constants.VIDEO.equals(suffixName) )){
            suffixName = Constants.OTHER_FILE;
        }
        String filePath = new File(suffixName).getAbsolutePath();
        File fileUpload = new File(filePath);
        if (!fileUpload.exists()) {
            fileUpload.mkdirs();
        }

        fileUpload = new File(filePath, fileName);
        if (fileUpload.exists()) {
            throw new Exception("上传的日志文件已存在");
        }

        try {
            file.transferTo(fileUpload);
            String pathUrl = fileUpload.getAbsolutePath();
            System.out.println(pathUrl);
            return pathUrl;
        } catch (IOException e) {
            throw new Exception("上传日志文件到服务器失败：" + e.toString());
        }
    }


    public static void main(String[] args) {
        //静态文件获取方式
        File fil = new File(staticPath + "text/111.txt");
        System.out.println(fil.getAbsolutePath());
        System.out.println(fil.exists());
        System.out.println(File.separator);

        String a = "wdaa.jpg";
        String substring = a.substring(a.lastIndexOf(".") + 1);
        System.out.println(substring);
    }


}
